package org.wattdepot.cli.commands;

import java.util.ArrayList;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Provides the SensorData for a given day.
 * 
 * @author Paul Galiza
 * 
 */
public class SensorDataDayCommand implements Command {
  // For testing the time stamps
  private List<XMLGregorianCalendar> testTstamps = new ArrayList<XMLGregorianCalendar>();

  /**
   * Used to test the time stamp increments.
   * 
   * @return Returns the incremented time stamps.
   */
  protected List<XMLGregorianCalendar> getTestTstamps() {
    return testTstamps;
  }

  /**
   * Calls the listSensorDataDay method.
   * 
   * @param userInput The user input.
   * @param client The WattDepotClient
   * @return Returns the method call with the proper parameters.
   */
  public String doCommand(List<String> userInput, WattDepotClient client) {
    return listSensorDataDay(userInput, client);
  }

  /**
   * Returns a string listing all of the sensor data instances for the specified day.
   * 
   * @param commandParam The input from user.
   * @param client The WattDepotClient
   * @return Returns the sensor data information.
   */
  private String listSensorDataDay(List<String> commandParam, WattDepotClient client) {
    StringBuffer buff = new StringBuffer(200);

    // Retrieves the source and day from the command
    String source = commandParam.get(1);
    String day = commandParam.get(3);

    // Checks to see if source is valid
    SourceChecker check = new SourceChecker();
    if (!check.sourceChecker(commandParam.get(1), client)) {
      return "Invalid Source";
    }

    try {
      // Checks to see if the time stamp is valid
      TStampChecker timeStamp = new TStampChecker();
      if (!timeStamp.checkTStamp(day)) {
        return "Invalid timestamp";
      }

      XMLGregorianCalendar inputDay = Tstamp.makeTimestamp(day);
      XMLGregorianCalendar endDay = Tstamp.incrementDays(inputDay, 1);

      while (!inputDay.equals(endDay)) {
        buff.append(inputDay.toString());
        buff.append("  Tool: ");
        buff.append(client.getSensorData(source, inputDay).getTool());
        buff.append("  Properties: (powerGenerated : ");
        buff.append(client.getPowerGenerated(source, inputDay));
        buff.append(")\n");

        // adds to time stamps list for testing purposes
        testTstamps.add(inputDay);

        // updates the inputDay to incremented hour
        inputDay = Tstamp.incrementHours(inputDay, 6);
      }
    }
    catch (NotAuthorizedException e) {
      e.printStackTrace();
    }
    catch (ResourceNotFoundException e) {
      System.out.println("Error: Requested timestamp " + commandParam.get(3) + " for the source "
          + commandParam.get(1) + " not found.");
      return "";
    }
    catch (BadXmlException e) {
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      e.printStackTrace();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    return buff.toString();
  }
}
